﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vconf_shared;

namespace vconf_client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer_heartbeat.Start();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            VConfClient.Unregister();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            VConfClient.getUsers();
            listBox1.Enabled = true;
            try
            {
                listBox1.DataSource = new BindingSource(VConfClient.users, null);
                listBox1.DisplayMember = "Key";
                listBox1.ValueMember = "Value";
            }
            catch (System.ArgumentException ae)
            {
                Console.WriteLine("Failed to get users OR got 0 users... " + ae.StackTrace);
            }
            
           
        }

        private void button_connect_Click(object sender, EventArgs e)
        {
            button_users.Enabled = true;
            ShowHideCamButton.Enabled = true;
            

            int port = int.Parse(portBox.Text);
            string addr = ipBox.Text;
            VConfClient.port = port;
            VConfClient.srvAddress = addr;
            VConfClient.connect();
            VConfClient.createUser(userBox.Text);

            if (!VConfClient.Register())
            {
                MessageBox.Show("Nie udało się zarejestrować na serwerze. Najprawdopodobniej taki użytkownik już istnieje.");
            }
            else
            {
                statusLabel_connectionStatus.Text = "Połączono z serwerem";
                videoGrabber2.Visible = true;
            }
            //VConfClient.getUsers();
            startOurVideo();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
        //MessageBox.Show(listBox1.SelectedItem.ToString());

                User u;
                Boolean isOk = true;
                KeyValuePair<String, User> ku = (KeyValuePair<String, User>)listBox1.SelectedItem;
                u = (User)ku.Value;
                //Boolean isOk = VConfClient.users.TryGetValue(listBox1.SelectedItem.ToString(), out u);
                //MessageBox.Show("Selected item is of type " + listBox1.SelectedItem.GetType());
                //String lol = "Selected user: " + (String)listBox1.SelectedItem + ", associated user object's name: "
                 //   + (u == null ? "null" : u.Name);
                MessageBox.Show("Selected user: " + u.Name);
                if (isOk)
                {
                    startComradeVideo(u.StreamURL);
                }
                else
                {
                    MessageBox.Show("Failed to connect to user " + listBox1.SelectedItem.ToString());                               
                }
                
            }
        }

        /**
         * Uruchamia nasze wideo
         */
        private void startOurVideo()
        {
            Console.WriteLine("starting preview... On device '0'");


            //this.videoGrabber2.JPEGPerformance = VidGrab.TJPEGPerformance.jpBestSpeed;
            //this.videoGrabber2.JPEGProgressiveDisplay = false;
            //this.videoGrabber2.JPEGQuality = 60;
            //videoGrabber2.FrameRate = 20;
            //this.videoGrabber2.RecordingOnMotion_NoMotionPauseDelayMs = 0;
            // this.videoGrabber2.Version = "v8.6.1.2 (build 4394) - November 21, 2011 - Copyright ©2011 Datastead";

            this.videoGrabber2.OnDirectNetworkStreamingHostUrl += new VidGrab.OnDirectNetworkStreamingHostUrlEventHandler(videoGrabber2_OnDirectNetworkStreamingHostUrl);
            this.videoGrabber2.VideoDevice = 0;
            this.videoGrabber2.NetworkStreaming = VidGrab.TNetworkStreaming.ns_ASFDirectNetworkStreaming;
            this.videoGrabber2.NetworkStreamingType = VidGrab.TNetworkStreamingType.nst_VideoStreaming;
            this.videoGrabber2.ASFNetworkPort = 0;
            this.videoGrabber2.StartPreview();
        }

        void videoGrabber2_OnDirectNetworkStreamingHostUrl(object sender, VidGrab.TOnDirectNetworkStreamingHostUrlEventArgs e)
        {
            VConfClient.AssignStreamURL(e.hostUrl);
            this.status_streamURL.Text = e.hostUrl;
        }

        /**
         * uruchamia wideo ziomka, do ktorego sie podlaczylismy
         */
        private void startComradeVideo(String url)
        {
            this.VideoGrabber1.Visible = true;
            //videoSent = new VidGrabNoForm.VideoGrabber();
            this.VideoGrabber1.VideoSource = VidGrab.TVideoSource.vs_VideoFileOrURL;
            this.VideoGrabber1.VideoSource_FileOrURL = url;
            this.VideoGrabber1.StartPreview();
            //videoSent.VideoSource = VidGrabNoForm.vs_VideoFileOrURL;



        }

        private void timer_heartbeat_Tick(object sender, EventArgs e)
        {
            VConfClient.SendHeartBeat();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (videoGrabber2.Visible == true)
            {
                videoGrabber2.Visible = false;
                ShowHideCamButton.Text = "Pokaż swoj obraz";
            }
            else
            {
                videoGrabber2.Visible = true;
                ShowHideCamButton.Text = "Ukryj swoj obraz";
            }
        }

    }
}
